package org.example.com.editor.command.workspaceCommand;

import org.example.com.editor.command.Command;
import org.example.com.editor.logger.LogManager;
import org.example.com.editor.ui.ConsoleUI;
import org.example.com.editor.workspace.Workspace;

public class EditCommand implements Command {
    private final LogManager logManager;

    public EditCommand(LogManager logManager) {
        this.logManager = logManager;
    }
    @Override
    public void execute(Workspace workspace, ConsoleUI ui) {
        String[] args = ui.getCommandArgs();
        if (args.length != 2) {
            ui.printError("用法：edit <文件路径>");
        }else{
            String filePath = args[1];
            ui.printInfo("编辑文件：" + filePath);
            try{
                workspace.editFile(filePath);
                logManager.onCommandExecuted(filePath, args);
                ui.printInfo("当前活动文件已切换为：" + filePath);
            }catch (Exception e){
                ui.printError(e.getMessage());
            }
        }
    }

    @Override
    public String getName() {
        return "edit";
    }
}
